<?php
namespace App\Repositories;

use App\Models\AdminMenu;

class AdminMenuRepository
{
    /**
     * 获取所有菜单
     * @return [type] [description]
     */
	public function getAllMenus($only_menus = false)
    {
        $menus = AdminMenu::where('parent_id', 0)->where('status', 1)
            ->when($only_menus, function($query) {
                return $query->where('is_menu', 1);
            })
            ->orderBy('listorder','asc')->get();
        $data = [];

        foreach ($menus as $menu) {
            $subMenus = $this->subMenus($menu->id, $only_menus);
            $d = [
                'id'          => $menu->id,
                'title'       => $menu->title,
                'route'       => $menu->route,
                'icon'        => $menu->icon,
                'description' => $menu->description,
                'listorder'   => $menu->listorder,
                'status'      => $menu->status
            ];
            if ($subMenus) $d['sub'] = $subMenus;
            $data[] = $d;
        }
        return $data;
    }

    /**
     * 获取顶级菜单
     * @return [type] [description]
     */
    public function getTopMenus($except_id = null)
    {
        if (is_null($except_id)){
            $menus = AdminMenu::where('parent_id', 0)->where('status', 1)->orderBy('listorder','asc')->get();
        } else {
            $menus = AdminMenu::where('parent_id', 0)->where('status', 1)->where('id', '<>', $except_id)->orderBy('listorder', 'asc')->get();
        }
        return $menus;
    }

    /**
     * 获取子菜单
     * @param  [type] $menu_id [description]
     * @return [type]          [description]
     */
    private function subMenus($menu_id, $only_menus = false)
    {
        $menus = AdminMenu::where('parent_id', $menu_id)->where('status', 1)
            ->when($only_menus, function($query) {
                return $query->where('is_menu', 1);
            })
            ->orderBy('listorder', 'asc')->get();
        $data = [];

        if ($menus) {
            foreach ($menus as $menu) {
                $d = [
                    'id'          => $menu->id,
                    'title'       => $menu->title,
                    'route'       => $menu->route,
                    'icon'        => $menu->icon,
                    'description' => $menu->description,
                    'listorder'   => $menu->listorder,
                    'status'      => $menu->status
                ];
                $data[] = $d;
            }
        }
        return $data;
    }

    /**
     * 菜单权限列表
     * @return [type] [description]
     */
    public function getMenuLists()
    {
        $mainMenus = AdminMenu::where('parent_id', 0)->orderBy('listorder','desc')->get();
        $data = [];
        foreach ($mainMenus as $main) {
            $data[] = [
                'id'          => $main->id,
                'parent'      => '',
                'title'       => $main->title,
                'route'       => $main->route,
                'icon'        => $main->icon,
                'description' => $main->description,
                'listorder'   => $main->listorder,
                'status'      => $main->status,
                'is_menu'     => $main->is_menu
            ];

            $menus = AdminMenu::where('parent_id', $main->id)->orderBy('listorder', 'desc')->get();
            if ($menus) {
                foreach ($menus as $menu) {
                    $data[] = [
                        'id'          => $menu->id,
                        'parent'      => '-- ',
                        'title'       => $menu->title,
                        'route'       => $menu->route,
                        'icon'        => $menu->icon,
                        'description' => $menu->description,
                        'listorder'   => $menu->listorder,
                        'status'      => $menu->status,
                        'is_menu'     => $menu->is_menu
                    ];
                }
            }
        }
        return collect($data);
    }

    /**
     * 添加菜单
     * @param [type] $request [description]
     */
    public function addMenu($request)
    {
        $menu = AdminMenu::create([
            'parent_id'   => $request->parent_id,
            'title'       => $request->title,
            'icon'        => isset($request->icon) ? $request->icon : '&#xe621;',
            'route'       => $request->route,
            'description' => $request->description,
            'is_menu'     => $request->is_menu,
            'status'      => $request->status,
            'listorder'   => $request->listorder
        ]);
        return $menu;
    }

    /**
     * 更新菜单
     * @param  [type] $request [description]
     * @param  [type] $id      [description]
     * @return [type]          [description]
     */
    public function updateMenu($request, $id)
    {
        $menu = AdminMenu::find($id);
        $menu->parent_id   = $request->parent_id;
        $menu->title       = $request->title;
        $menu->icon        = isset($request->icon) ? $request->icon : '&#xe621;';
        $menu->route       = $request->route;
        $menu->description = $request->description;
        $menu->is_menu     = $request->is_menu;
        $menu->status      = $request->status;
        $menu->listorder   = $request->listorder;

        $menu->save();
    }

    /**
     * 删除菜单
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function deleteMenu($id)
    {
        return AdminMenu::find($id)->delete();
    }

    /**
     * 批量删除
     * @param  [type] $ids [description]
     * @return [type]      [description]
     */
    public function deleteMenuLists($ids)
    {
        return AdminMenu::whereIn('id',$ids)->delete();
    }
}